<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" v-if="!inSide">
      <el-form-item label="转换人员" prop="userId">
        <user-drug-select v-model="queryParams.userId" placeholder="请选择转换人员" :onchange="handleQuery"/>
      </el-form-item>
      <el-form-item label="转换类型" prop="changeType">
        <el-select v-model="queryParams.changeType" placeholder="请选择转换类型" clearable>
          <el-option
            v-for="dict in dict.type.ryzhlx"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" v-if="!inSide">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['event:changeType:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['event:changeType:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['event:changeType:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['event:changeType:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="changeTypeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" v-if="!inSide"/>
      <el-table-column label="主键" align="center" prop="typeId" v-if="false"/>
      <el-table-column label="转换人员" align="center" prop="userInfo.name" v-if="!inSide"/>
      <el-table-column label="转换日期" align="center" prop="createTime" v-if="inSide"/>
      <el-table-column label="身份证" align="center" prop="userInfo.idno" v-if="!inSide"/>
      <el-table-column label="转换类型" align="center" prop="changeType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ryzhlx" :value="scope.row.changeType"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" v-if="!inSide"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['event:changeType:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['event:changeType:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改人员类型转换对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-alert title="只有低风险社戒社康人员可以进行类型转换" type="error" style="margin-bottom: 20px;">
      </el-alert>
      <el-form ref="form" :model="form" :rules="getRules()" label-width="100px">
        <el-form-item label="转换人员" prop="userId">
          <user-drug-select v-model="form.userId" placeholder="请选择转换人员" :readonly="inSide" :risk-level="'3'"/>
        </el-form-item>
        <el-form-item label="转换类型" prop="changeType">
          <el-select v-model="form.changeType" placeholder="请选择转换类型" @change="handleTypeChange" style="width: 100%">
            <el-option
              v-for="dict in dict.type.ryzhlx"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="决定机关" prop="jdjg" v-if="isFieldNeedToShow('jdjg')">
          <el-input v-model="form.jdjg" placeholder="请输入决定机关" />
        </el-form-item>
        <el-form-item label="决定时间" prop="jdsj" v-if="isFieldNeedToShow('jdsj')">
          <el-date-picker clearable
            v-model="form.jdsj"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择决定时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="进(入)所时间" prop="rssj" v-if="isFieldNeedToShow('rssj')">
          <el-date-picker clearable
                          v-model="form.rssj"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择决定时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="出所时间" prop="cssj" v-if="isFieldNeedToShow('cssj')">
          <el-date-picker clearable
            v-model="form.cssj"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择出所时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="戒毒场所" prop="jdcs" v-if="isFieldNeedToShow('jdcs')">
          <el-input v-model="form.jdcs" placeholder="请输入戒毒场所" />
        </el-form-item>
        <el-form-item label="强戒原因" prop="qjyy" v-if="isFieldNeedToShow('qjyy')">
          <el-input v-model="form.qjyy" placeholder="请输入强戒原因" />
        </el-form-item>
        <el-form-item label="入所原因" prop="rsyy" v-if="isFieldNeedToShow('rsyy')">
          <el-input v-model="form.rsyy" placeholder="请输入入所原因" />
        </el-form-item>
        <el-form-item label="羁押地点" prop="jsdd" v-if="isFieldNeedToShow('jsdd')">
          <el-input v-model="form.jsdd" placeholder="请输入羁押地点" />
        </el-form-item>
        <el-form-item label="入狱时间" prop="rysj" v-if="isFieldNeedToShow('rysj')">
          <el-date-picker clearable
            v-model="form.rysj"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择入狱时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="判刑地点" prop="pxdd" v-if="isFieldNeedToShow('pxdd')">
          <el-input v-model="form.pxdd" placeholder="请输入判刑地点" />
        </el-form-item>
        <el-form-item label="出狱时间" prop="cysj" v-if="isFieldNeedToShow('cysj')">
          <el-date-picker clearable
            v-model="form.cysj"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择出狱时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="服刑地区" prop="fxdq" v-if="isFieldNeedToShow('fxdq')">
          <el-input v-model="form.fxdq" placeholder="请输入服刑地区" />
        </el-form-item>
        <el-form-item label="监狱名称" prop="jymc" v-if="isFieldNeedToShow('jymc')">
          <el-input v-model="form.jymc" placeholder="请输入监狱名称" />
        </el-form-item>
        <el-form-item label="入狱原因" prop="ryyy" v-if="isFieldNeedToShow('ryyy')">
          <el-input v-model="form.ryyy" placeholder="请输入入狱原因" />
        </el-form-item>
        <el-form-item label="执行时间" prop="zxsj" v-if="isFieldNeedToShow('zxsj')">
          <el-date-picker clearable
            v-model="form.zxsj"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择执行时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="治疗时间" prop="zlsj" v-if="isFieldNeedToShow('zlsj')">
          <el-date-picker clearable
            v-model="form.zlsj"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择治疗时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="治疗机构" prop="zljg" v-if="isFieldNeedToShow('zljg')">
          <el-input v-model="form.zljg" placeholder="请输入治疗机构" />
        </el-form-item>
        <el-form-item label="离开时间" prop="lksj" v-if="isFieldNeedToShow('lksj')">
          <el-date-picker clearable
            v-model="form.lksj"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择离开时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="死亡时间" prop="swsj" v-if="isFieldNeedToShow('swsj')">
          <el-date-picker clearable
            v-model="form.swsj"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择死亡时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="死亡原因" prop="swyy" v-if="isFieldNeedToShow('swyy')">
          <el-input v-model="form.swyy" placeholder="请输入死亡原因" />
        </el-form-item>
        <el-form-item label="死亡地点" prop="swdd" v-if="isFieldNeedToShow('swdd')">
          <el-input v-model="form.swdd" placeholder="请输入死亡地点" />
        </el-form-item>
        <el-form-item label="结束时间" prop="jssj" v-if="isFieldNeedToShow('jssj')">
          <el-date-picker clearable
            v-model="form.jssj"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="原因详情" prop="yyxq" v-if="isFieldNeedToShow('yyxq')">
          <el-input v-model="form.yyxq" placeholder="请输入原因详情" />
        </el-form-item>
        <el-form-item label="拘留地点" prop="jldd" v-if="isFieldNeedToShow('jldd')">
          <el-input v-model="form.jldd" placeholder="请输入拘留地点" />
        </el-form-item>
        <el-form-item label="决定书上传" prop="jdsFiles" v-if="isFieldNeedToShow('jdsFiles')">
          <file-upload v-model="form.jdsFiles" :file-type="['pdf','doc', 'docx']"/>
        </el-form-item>
        <el-form-item label="精神病院" prop="jsby" v-if="isFieldNeedToShow('jsby')">
          <el-input v-model="form.jsby" placeholder="请输入精神病院" />
        </el-form-item>
        <el-form-item label="入院日期" prop="ryrq" v-if="isFieldNeedToShow('ryrq')">
          <el-date-picker clearable
            v-model="form.ryrq"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择入院日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="出院日期" prop="cyrq" v-if="isFieldNeedToShow('cyrq')">
          <el-date-picker clearable
            v-model="form.cyrq"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择出院日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="撤销时间" prop="cxsj" v-if="isFieldNeedToShow('cxsj')">
          <el-date-picker clearable
            v-model="form.cxsj"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择撤销时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="文书号" prop="wsh" v-if="isFieldNeedToShow('wsh')">
          <el-input v-model="form.wsh" placeholder="请输入文书号" />
        </el-form-item>
        <el-form-item label="备注" prop="remark" v-if="isFieldNeedToShow('remark')">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listChangeType, getChangeType, delChangeType, addChangeType, updateChangeType } from "@/api/event/changeType";
import {typeToFieldMap} from "@/views/event/changeType/change-type-field-map";
import sideView from "@/views/event/mixin/sideView";

export default {
  name: "ChangeType",
  dicts: ['ryzhlx'],
  mixins: [sideView],
  data() {
    return {
      typeToFieldMap,
      rulesChange: true,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人员类型转换表格数据
      changeTypeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        changeType: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rulesBasic: {
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        changeType: [
          { required: true, message: "转换类型不能为空", trigger: "change" }
        ],
      },
      rulesExtra: {
        jdjg: [
          { required: true, message: "决定机关不能为空", trigger: "blur" }
        ],
        jdsj: [
          { required: true, message: "决定时间不能为空", trigger: "blur" }
        ],
        rssj: [
          { required: true, message: "进所时间不能为空", trigger: "blur" }
        ],
        jdcs: [
          { required: true, message: "戒毒场所不能为空", trigger: "blur" }
        ],
        rsyy: [
          { required: true, message: "入所原因不能为空", trigger: "blur" }
        ],
        jsdd: [
          { required: true, message: "羁押地点不能为空", trigger: "blur" }
        ],
        rysj: [
          { required: true, message: "入狱时间不能为空", trigger: "blur" }
        ],
        pxdd: [
          { required: true, message: "判刑地点不能为空", trigger: "blur" }
        ],
        fxdq: [
          { required: true, message: "服刑地区不能为空", trigger: "blur" }
        ],
        jymc: [
          { required: true, message: "监狱名称不能为空", trigger: "blur" }
        ],
        ryyy: [
          { required: true, message: "入狱原因不能为空", trigger: "blur" }
        ],
        zxsj: [
          { required: true, message: "执行时间不能为空", trigger: "blur" }
        ],
        zlsj: [
          { required: true, message: "治疗时间不能为空", trigger: "blur" }
        ],
        zljg: [
          { required: true, message: "治疗机构不能为空", trigger: "blur" }
        ],
        lksj: [
          { required: true, message: "离开时间不能为空", trigger: "blur" }
        ],
        swsj: [
          { required: true, message: "死亡时间不能为空", trigger: "blur" }
        ],
        swdd: [
          { required: true, message: "死亡地点不能为空", trigger: "blur" }
        ],
        yyxq: [
          { required: true, message: "原因详情不能为空", trigger: "blur" }
        ],
        jldd: [
          { required: true, message: "拘留地点不能为空", trigger: "blur" }
        ],
        jdsFiles: [
          { required: true, message: "决定书上传不能为空", trigger: "blur" }
        ],
        jsby: [
          { required: true, message: "精神病院不能为空", trigger: "blur" }
        ],
        ryrq: [
          { required: true, message: "入院日期不能为空", trigger: "blur" }
        ],
        wsh: [
          { required: true, message: "文书号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleTypeChange() {
      this.rulesChange = false
      this.$nextTick(()=> {
        this.rulesChange = true
      })
    },
    getRules() {
      Object.keys(this.rulesExtra).forEach(prop=> {
        this.rulesExtra[prop].required= this.isFieldNeedToShow(prop)
      })
      return {...this.rulesBasic, ...this.rulesExtra}
    },
    isFieldNeedToShow(prop) {
      return this.form.changeType && this.typeToFieldMap[prop].includes(parseInt(this.form.changeType))
    },
    /** 查询人员类型转换列表 */
    getList() {
      this.loading = true;
      this.handleWhenInSide((userId)=> {
        this.queryParams.userId = userId
      })
      listChangeType(this.queryParams).then(response => {
        this.changeTypeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        typeId: undefined,
        userId: undefined,
        changeType: undefined,
        jdjg: undefined,
        jdsj: undefined,
        rssj: undefined,
        cssj: undefined,
        jdcs: undefined,
        qjyy: undefined,
        remark: undefined,
        rsyy: undefined,
        jsdd: undefined,
        rysj: undefined,
        pxdd: undefined,
        cysj: undefined,
        fxdq: undefined,
        jymc: undefined,
        ryyy: undefined,
        zxsj: undefined,
        zlsj: undefined,
        zljg: undefined,
        lksj: undefined,
        swsj: undefined,
        swyy: undefined,
        swdd: undefined,
        jssj: undefined,
        yyxq: undefined,
        jldd: undefined,
        jdsFiles: undefined,
        jsby: undefined,
        ryrq: undefined,
        cyrq: undefined,
        cxsj: undefined,
        wsh: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.typeId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加人员类型转换";
      this.handleWhenInSide((userId)=> {
        this.form.userId = userId
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const typeId = row.typeId || this.ids
      getChangeType(typeId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改人员类型转换";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.typeId != null) {
            updateChangeType(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addChangeType(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const typeIds = row.typeId || this.ids;
      this.$modal.confirm('是否确认删除人员类型转换编号为"' + typeIds + '"的数据项？').then(() => {
        this.loading = true;
        return delChangeType(typeIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('event/changeType/export', {
        ...this.queryParams
      }, `changeType_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
